package com.github.androiddevelopmentframework.script.presenter;

import android.content.Intent;

import com.github.androiddevelopmentframework.script.bean.UserInfo;
import com.github.androiddevelopmentframework.script.mvpInterface.ScriptInterface;
import com.github.freewu.commons.utils.ToastUtil;
import com.github.freewu.mvp.presenter.Presenter;
import com.github.freewu.script.ScriptExecCallback;
import com.github.freewu.script.js.JSRuntime;

/**
 *
 * Created by free on 2017/1/16 0016.
 */
public class ScriptPresenter extends Presenter<ScriptInterface.ViewInterface> implements ScriptExecCallback {
    UserInfo userInfo;

    public ScriptPresenter(ScriptInterface.ViewInterface mView) {
        super(mView);
    }

    @Override
    public void init(Intent intent) {
        userInfo=new UserInfo();

        JSRuntime.create(JSRuntime.RHINO)
                .addGlobalVariable("userInfo",userInfo)
                .addScript(getView().getContext().getAssets(),"js/configure.js")
                .setScriptExecCallback(this)
                .execute();
    }

    @Override
    public void onSuccess() {
        getView().setUserInfo(userInfo);
    }

    @Override
    public void onError(Exception e) {
        ToastUtil.showShort(getView().getContext(),"js执行出错"+e.toString());
    }
}
